本文共 26843 字,大约阅读时间需要 89 分钟。
BaseDictDao.xml
BaseDictDao 接口
package com.itheima.core.dao;import java.util.List;import com.itheima.core.po.BaseDict;/** * 数据字典 */public interface BaseDictDao { // 根据类别代码查询数据字典 public ListselectBaseDictByTypeCode(String typecode);}
UserDao.xml
UserDao 接口
package com.itheima.core.dao;import org.apache.ibatis.annotations.Param;import com.itheima.core.po.User;/** * 用户DAO层接口 */public interface UserDao { /** * 通过账号和密码查询用户 */ public User findUser(@Param("usercode") String usercode, @Param("password") String password);}
CustomerDao.xml
cust_name like "%"#{ cust_name}"%" and cust_source = #{ cust_source} and cust_industry = #{ cust_industry} and cust_level = #{ cust_level} insert into customer( cust_name, cust_user_id, cust_create_id, cust_source, cust_industry, cust_level, cust_linkman, cust_phone, cust_mobile, cust_zipcode, cust_address, cust_createtime ) values(#{ cust_name}, #{ cust_user_id}, #{ cust_create_id}, #{ cust_source}, #{ cust_industry}, #{ cust_level}, #{ cust_linkman}, #{ cust_phone}, #{ cust_mobile}, #{ cust_zipcode}, #{ cust_address}, #{ cust_createtime} ) update customer where cust_id=#{ cust_id} cust_name=#{ cust_name}, cust_user_id=#{ cust_user_id}, cust_create_id=#{ cust_create_id}, cust_source=#{ cust_source}, cust_industry=#{ cust_industry}, cust_level=#{ cust_level}, cust_linkman=#{ cust_linkman}, cust_phone=#{ cust_phone}, cust_mobile=#{ cust_mobile}, cust_zipcode=#{ cust_zipcode}, cust_address=#{ cust_address}, cust_createtime=#{ cust_createtime}, delete from customer where cust_id=#{ id}
CustomerDao
package com.itheima.core.dao;import java.util.List;import com.itheima.core.po.Customer;/** * Customer接口 */public interface CustomerDao { // 客户列表 public ListselectCustomerList(Customer customer); // 客户数 public Integer selectCustomerListCount(Customer customer); // 创建客户 public int createCustomer(Customer customer); // 通过id查询客户 public Customer getCustomerById(Integer id); // 更新客户信息 public int updateCustomer(Customer customer); // 删除客户 int deleteCustomer (Integer id);}
NavigationTag
package com.itheima.common.utils;import java.io.IOException;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;/** * 显示格式:首页 上一页 1 2 3 4 5下一页 尾页 */public class NavigationTag extends TagSupport { static final long serialVersionUID = 2372405317744358833L; /** * request 中用于保存Page对象的变量名,默认为“page” */ private String bean = "page"; /** * 分页跳转的url地址,此属性必须 */ private String url = null; /** * 显示页码数量 */ private int number = 5; @Override public int doStartTag() throws JspException { JspWriter writer = pageContext.getOut(); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); Page page = (Page) request.getAttribute(bean); if (page == null) return SKIP_BODY; url = resolveUrl(url, pageContext); try { // 计算总页数 int pageCount = page.getTotal() / page.getSize(); if (page.getTotal() % page.getSize() > 0) { pageCount++; } writer.print(" "); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } private String append(String url, String key, int value) { return append(url, key, String.valueOf(value)); } /** * 为url 参加参数对儿 */ private String append(String url, String key, String value) { if (url == null || url.trim().length() == 0) { return ""; } if (url.indexOf("?") == -1) { url = url + "?" + key + "=" + value; } else { if (url.endsWith("?")) { url = url + key + "=" + value; } else { url = url + "&" + key + "=" + value; } } return url; } /** * 为url 添加翻页请求参数 */ private String resolveUrl(String url, javax.servlet.jsp.PageContext pageContext) throws JspException { Map params = pageContext.getRequest().getParameterMap(); for (Object key : params.keySet()) { if ("page".equals(key) || "rows".equals(key)){ continue; } Object value = params.get(key); if (value == null){ continue; } if (value.getClass().isArray()) { url = append(url, key.toString(), ((String[]) value)[0]); } else if (value instanceof String) { url = append(url, key.toString(), value.toString()); } } return url; } public String getBean() { return bean; } public void setBean(String bean) { this.bean = bean; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public void setNumber(int number) { this.number = number; }}
Page
package com.itheima.common.utils;import java.util.List;public class Page{ private int total; // 总条数 private int page; // 当前页 private int size; // 每页数 private List rows; // 结果集 public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } }
LoginInterceptor
package com.itheima.core.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import com.itheima.core.po.User;/** * 登录拦截器 */public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 获取请求的URL String url = request.getRequestURI(); // URL:除了登录请求外,其他的URL都进行拦截控制 if (url.indexOf("/login.action") >= 0) { return true; } // 获取Session HttpSession session = request.getSession(); User user = (User) session.getAttribute("USER_SESSION"); // 判断Session中是否有用户数据,如果有,则返回true,继续向下执行 if (user != null) { return true; } // 不符合条件的给出提示信息,并转发到登录页面 request.setAttribute("msg", "您还没有登录,请先登录!"); request.getRequestDispatcher("/WEB-INF/jsp/login.jsp") .forward(request, response); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }}
BaseDict
package com.itheima.core.po;import java.io.Serializable;/** * 数据字典持久化类 */public class BaseDict implements Serializable { private static final long serialVersionUID = 1L; private String dict_id; // 数据字典id private String dict_type_code; // 数据字典类别代码 private String dict_type_name; // 数据字典类别名称 private String dict_item_name; // 数据字典项目名称 private String dict_item_code; // 数据字典项目代码 private Integer dict_sort; // 排序字段 private String dict_enable; // 是否可用 private String dict_memo; // 备注 public String getDict_id() { return dict_id; } public void setDict_id(String dict_id) { this.dict_id = dict_id; } public String getDict_type_code() { return dict_type_code; } public void setDict_type_code(String dict_type_code) { this.dict_type_code = dict_type_code; } public String getDict_type_name() { return dict_type_name; } public void setDict_type_name(String dict_type_name) { this.dict_type_name = dict_type_name; } public String getDict_item_name() { return dict_item_name; } public void setDict_item_name(String dict_item_name) { this.dict_item_name = dict_item_name; } public String getDict_item_code() { return dict_item_code; } public void setDict_item_code(String dict_item_code) { this.dict_item_code = dict_item_code; } public Integer getDict_sort() { return dict_sort; } public void setDict_sort(Integer dict_sort) { this.dict_sort = dict_sort; } public String getDict_enable() { return dict_enable; } public void setDict_enable(String dict_enable) { this.dict_enable = dict_enable; } public String getDict_memo() { return dict_memo; } public void setDict_memo(String dict_memo) { this.dict_memo = dict_memo; }}
Customer
package com.itheima.core.po;import java.io.Serializable;import java.util.Date;/** * 客户持久化类 */public class Customer implements Serializable { private static final long serialVersionUID = 1L; private Integer cust_id; // 客户编号 private String cust_name; // 客户名称 private Integer cust_user_id; // 负责人id private Integer cust_create_id; // 创建人id private String cust_source; // 客户信息来源 private String cust_industry; // 客户所属行业 private String cust_level; // 客户级别 private String cust_linkman; // 联系人 private String cust_phone; // 固定电话 private String cust_mobile; // 移动电话 private String cust_zipcode; // 邮政编码 private String cust_address; // 联系地址 private Date cust_createtime; // 创建时间 private Integer start; // 起始行 private Integer rows; // 所取行数 public String getCust_zipcode() { return cust_zipcode; } public void setCust_zipcode(String cust_zipcode) { this.cust_zipcode = cust_zipcode; } public String getCust_address() { return cust_address; } public void setCust_address(String cust_address) { this.cust_address = cust_address; } public Integer getStart() { return start; } public void setStart(Integer start) { this.start = start; } public Integer getRows() { return rows; } public void setRows(Integer rows) { this.rows = rows; } public Integer getCust_id() { return cust_id; } public void setCust_id(Integer cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public Integer getCust_user_id() { return cust_user_id; } public void setCust_user_id(Integer cust_user_id) { this.cust_user_id = cust_user_id; } public Integer getCust_create_id() { return cust_create_id; } public void setCust_create_id(Integer cust_create_id) { this.cust_create_id = cust_create_id; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } public Date getCust_createtime() { return cust_createtime; } public void setCust_createtime(Date cust_createtime) { this.cust_createtime = cust_createtime; }}
User
package com.itheima.core.po;import java.io.Serializable;/** * 用户持久化类 */public class User implements Serializable{ private static final long serialVersionUID = 1L; private Integer user_id; //用户id private String user_code; //用户账号 private String user_name; //用户名称 private String user_password; //用户密码 private Integer user_state; //用户状态 public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Integer getUser_state() { return user_state; } public void setUser_state(Integer user_state) { this.user_state = user_state; }}
BaseDictServiceImpl
package com.itheima.core.service.impl;import java.util.List;import com.itheima.core.dao.BaseDictDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.itheima.core.po.BaseDict;import com.itheima.core.service.BaseDictService;/** * 数据字典Service接口实现类 */@Service("baseDictService")public class BaseDictServiceImpl implements BaseDictService{ @Autowired private BaseDictDao baseDictDao; //根据类别代码查询数据字典 public ListfindBaseDictByTypeCode(String typecode) { return baseDictDao.selectBaseDictByTypeCode(typecode); }}
BaseDictService 接口
package com.itheima.core.service;import java.util.List;import com.itheima.core.po.BaseDict;/** * 数据字典Service接口 */public interface BaseDictService { //根据类别代码查询数据字典 public ListfindBaseDictByTypeCode(String typecode); }
CustomerServiceImpl
package com.itheima.core.service.impl;import java.util.List;import com.itheima.core.dao.CustomerDao;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.itheima.common.utils.Page;import com.itheima.core.po.Customer;import com.itheima.core.service.CustomerService;/** * 客户管理 */@Service("customerService")@Transactionalpublic class CustomerServiceImpl implements CustomerService { // 声明DAO属性并注入 @Autowired private CustomerDao customerDao; // 客户列表 public PagefindCustomerList(Integer page, Integer rows, String custName, String custSource,String custIndustry, String custLevel) { // 创建客户对象 Customer customer = new Customer(); // 判断客户名称 if(StringUtils.isNotBlank(custName)){ customer.setCust_name(custName); } // 判断客户信息来源 if(StringUtils.isNotBlank(custSource)){ customer.setCust_source(custSource); } // 判断客户所属行业 if(StringUtils.isNotBlank(custIndustry)){ customer.setCust_industry(custIndustry); } // 判断客户级别 if(StringUtils.isNotBlank(custLevel)){ customer.setCust_level(custLevel); } // 当前页 customer.setStart((page-1) * rows) ; // 每页数 customer.setRows(rows); // 查询客户列表 List customers = customerDao.selectCustomerList(customer); // 查询客户列表总记录数 Integer count = customerDao.selectCustomerListCount(customer); // 创建Page返回对象 Page result = new Page<>(); result.setPage(page); result.setRows(customers); result.setSize(rows); result.setTotal(count); return result; } /** * 创建客户 */ @Override public int createCustomer(Customer customer) { return customerDao.createCustomer(customer); } /** * 通过id查询客户 */ @Override public Customer getCustomerById(Integer id) { Customer customer = customerDao.getCustomerById(id); return customer; } /** * 更新客户 */ @Override public int updateCustomer(Customer customer) { return customerDao.updateCustomer(customer); } /** * 删除客户 */ @Override public int deleteCustomer(Integer id) { return customerDao.deleteCustomer(id); } }
CustomerService 接口
package com.itheima.core.service;import com.itheima.common.utils.Page;import com.itheima.core.po.Customer;public interface CustomerService { // 查询客户列表 public PagefindCustomerList(Integer page, Integer rows, String custName, String custSource, String custIndustry, String custLevel); public int createCustomer(Customer customer); // 通过id查询客户 public Customer getCustomerById(Integer id); // 更新客户 public int updateCustomer(Customer customer); // 删除客户 public int deleteCustomer(Integer id);}
UserServiceImpl
package com.itheima.core.service.impl;import com.itheima.core.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.itheima.core.po.User;import com.itheima.core.service.UserService;/** * 用户Service接口实现类 */@Service("userService")@Transactionalpublic class UserServiceImpl implements UserService { // 注入UserDao @Autowired private UserDao userDao; // 通过账号和密码查询用户 @Override public User findUser(String usercode, String password) { User user = this.userDao.findUser(usercode, password); return user; }}
UserService 接口
package com.itheima.core.service;import com.itheima.core.po.User;/** * 用户Service层接口 */public interface UserService { // 通过账号和密码查询用户 public User findUser(String usercode, String password);}
CustomerController
package com.itheima.core.web.controller;import java.sql.Timestamp;import java.util.Date;import java.util.List;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.itheima.common.utils.Page;import com.itheima.core.po.BaseDict;import com.itheima.core.po.Customer;import com.itheima.core.po.User;import com.itheima.core.service.BaseDictService;import com.itheima.core.service.CustomerService;/** * 客户管理控制器类 */@Controllerpublic class CustomerController { // 依赖注入 @Autowired private CustomerService customerService; @Autowired private BaseDictService baseDictService; // 客户来源 @Value("${customer.from.type}") private String FROM_TYPE; // 客户所属行业 @Value("${customer.industry.type}") private String INDUSTRY_TYPE; // 客户级别 @Value("${customer.level.type}") private String LEVEL_TYPE; /** * 客户列表 */ @RequestMapping(value = "/customer/list.action") public String list(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows, String custName, String custSource, String custIndustry, String custLevel, Model model) { // 条件查询所有客户 Pagecustomers = customerService .findCustomerList(page, rows, custName, custSource, custIndustry,custLevel); model.addAttribute("page", customers); // 客户来源 List fromType = baseDictService .findBaseDictByTypeCode(FROM_TYPE); // 客户所属行业 List industryType = baseDictService .findBaseDictByTypeCode(INDUSTRY_TYPE); // 客户级别 List levelType = baseDictService .findBaseDictByTypeCode(LEVEL_TYPE); // 添加参数 model.addAttribute("fromType", fromType); model.addAttribute("industryType", industryType); model.addAttribute("levelType", levelType); model.addAttribute("custName", custName); model.addAttribute("custSource", custSource); model.addAttribute("custIndustry", custIndustry); model.addAttribute("custLevel", custLevel); return "customer"; } /** * 创建客户 */ @RequestMapping("/customer/create.action") @ResponseBody public String customerCreate(Customer customer,HttpSession session) { // 获取Session中的当前用户信息 User user = (User) session.getAttribute("USER_SESSION"); // 将当前用户id存储在客户对象中 customer.setCust_create_id(user.getUser_id()); // 创建Date对象 Date date = new Date(); // 得到一个Timestamp格式的时间,存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss” Timestamp timeStamp = new Timestamp(date.getTime()); customer.setCust_createtime(timeStamp); // 执行Service层中的创建方法,返回的是受影响的行数 int rows = customerService.createCustomer(customer); if(rows > 0){ return "OK"; }else{ return "FAIL"; } } /** * 通过id获取客户信息 */ @RequestMapping("/customer/getCustomerById.action") @ResponseBody public Customer getCustomerById(Integer id) { Customer customer = customerService.getCustomerById(id); return customer; } /** * 更新客户 */ @RequestMapping("/customer/update.action") @ResponseBody public String customerUpdate(Customer customer) { int rows = customerService.updateCustomer(customer); if(rows > 0){ return "OK"; }else{ return "FAIL"; } } /** * 删除客户 */ @RequestMapping("/customer/delete.action") @ResponseBody public String customerDelete(Integer id) { int rows = customerService.deleteCustomer(id); if(rows > 0){ return "OK"; }else{ return "FAIL"; } }}
UserController
package com.itheima.core.web.controller;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.itheima.core.po.User;import com.itheima.core.service.UserService;/** * 用户控制器类 */@Controllerpublic class UserController { // 依赖注入 @Autowired private UserService userService; /** * 用户登录 */ @RequestMapping(value = "/login.action", method = RequestMethod.POST) public String login(String usercode,String password, Model model, HttpSession session) { // 通过账号和密码查询用户 User user = userService.findUser(usercode, password); if(user != null){ // 将用户对象添加到Session session.setAttribute("USER_SESSION", user); // 跳转到主页面// return "customer"; return "redirect:customer/list.action"; } model.addAttribute("msg", "账号或密码错误,请重新输入!"); // 返回到登录页面 return "login"; } /** * 模拟其他类中跳转到客户管理页面的方法 */ @RequestMapping(value = "/toCustomer.action") public String toCustomer() { return "customer"; } /** * 退出登录 */ @RequestMapping(value = "/logout.action") public String logout(HttpSession session) { // 清除Session session.invalidate(); // 重定向到登录页面的跳转方法 return "redirect:login.action"; } /** * 向用户登陆页面跳转 */ @RequestMapping(value = "/login.action", method = RequestMethod.GET) public String toLogin() { return "login"; }}
最后放一波项目源码链接
https://pan.baidu.com/s/1dMpPGGuGclMexgH-lMUKnQ
转载地址:http://ewywi.baihongyu.com/